package cn.i7baozh.starter.gateway.filter;

import cn.i7baozh.starter.gateway.constant.GatewayConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * 分布式 日志增强
 *
 * @author : baoqi.zhang
 * @date :   2022/5/11 15:08
 */
@Slf4j
@Component
public class DistributeLoggingFilter implements GlobalFilter, Ordered {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest();
        ServerHttpRequest build = request.mutate().header(GatewayConstants.DISTRIBUTE_LOGGING_FLAG, GatewayConstants.getUuid())
                .build();
        return chain.filter(exchange.mutate().request(build).build());
    }

    @Override
    public int getOrder() {
        return 1;
    }
}
